//
//  LinearList.c
//  数据结构
//
//  Created by 吴高烽 on 2018/4/11.
//  Copyright © 2018年 吴高烽. All rights reserved.
// 链表的所有操作前都要保证操作合法，即链表非空且索引不越界
// 线性表是逻辑结构，链表和顺序表是物理结构
// 定义节点指针
typedef void*  LinearListNodeValue;

typedef  struct  {
    int capacity; // 定义线性表的最大容量
    int length; // 定义线性表当前使用长度
    LinearListNodeValue *values; // 定义节点的类型
} LinearList; // 定义顺序表的类型
/* 可以防止语法检测的错误，语法检测针对当前文件，而编译是多个文件。当前文件没有错误，但在编译多个
 文件时会有变量名冲突的问题。
*/
#define LINEARLIST_STRUCT
#include "LinearList.h"
#include <stdlib.h>

LinearList *listCreat(int capacity){
    LinearList *list = malloc(sizeof(LinearList));
    list->capacity = capacity ;
    list->length = 0 ;
    // 指针如果有连续的空间就成为了数组
    list->values = malloc(capacity * sizeof(LinearListNodeValue));
    return list;
}
void listRelease(LinearList *list){
    free(list->values);
    free(list);
}
LinearListNodeValue listGet(LinearList *list , int index){
    if (list == NULL || index<0 || index>= list->length) {
        return 0;
    }
    return  list->values[index];
}
int listLength(LinearList *list){
    if (list==NULL) {
        return 0;
    }
    return  list->length;
}
void listInsert(LinearList *list, int index, LinearListNodeValue value){
    if (list==NULL || index <0 || index > list->length || list->length == list->capacity) {
        return ;
    }
    // 从后开始复制数据
    for (int i = list->length-1; i>=index; i--) {
        list->values[i+1] = list->values[i];
    }
    list->values[index] = value;
    list->length++;
}

void listAdd(LinearList *list , LinearListNodeValue value){
    if (list==NULL) {
        return ;
    }
    listInsert(list, list->length, value);
}
// 更具索引修改对应的值
void listSet(LinearList *list , int index, LinearListNodeValue value){
    if (list==NULL || index<0 || index >= list->length) {
        return ;
    }
    list->values[index] = value;
}
void listRemov(LinearList *list, int index){
    if (list == NULL || index < 0 || index >= list->length) {
        return ;
    }
    for (int i = index+1; i<list->length; i++) {
        list->values[i-1] = list->values[i];
    }
    list->length--;
}
/**删除某个值的所有数据 **/
void listRemovValue(LinearList *list , LinearListNodeValue value){
    if (list == NULL) {
        return ;
    }
    int removCount = 0;
    for (int i = 0 ; i < list->length; i++) {
        if (list->values[i] == value) { //需要删除
            removCount++;
        }else{ // 不需要删除
            list->values[i-removCount] = list->values[i];
        }
    }
    list->length -= removCount ;
}
bool Empty(LinearList *list){
    if (list == NULL || list->length == 0) {
        return true;
    }else{
        return false;
    }
}
// -1 表示不存在
int locateElem(LinearList *list, LinearListNodeValue value){
    if (Empty(list)==true) {
        return  -1;
    }
//    地址比较
//    for (int i = 0; i<list->length; i++) {
//        if (list->values[i] ==value) {
//            return i;
//        }
//    }
    
//    值比较
    int* p = value;
    for (int i = 0; i<list->length; i++) {
        int* q =list->values[i];
        if (*p==*q) {
            return i;
        }
    }
    return  -1;
}

void listgPrint(LinearList *list){
    if (Empty(list)==true) {
        return ;
    }
    for (int i = 0; i <list->length; i++) {
        int *p = list->values[i];
        printf("%d ",*p);
    }
    printf("\n");
}

void listDelete(LinearList *list,int index, LinearListNodeValue* value){
    if (list == NULL || index < 0 || index >= list->length) {
        return ;
    }
    *value = list->values[index];
    for (; index<list->length; index++) {
        list->values[index] = list->values[index+1];
    }
    list->length--;
    return;
}
void listDlte(LinearList *list,int index, LinearListNodeValue value){
    if (list == NULL || index < 0 || index >= list->length) {
        return ;
    }
    int *p = list->values[index];
    value = p ;
    for (; index<list->length; index++) {
        list->values[index] = list->values[index+1];
    }
    list->length--;
    return;
}
